package elitech.vietnam.myfashion.fragment;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.extend.AsyncTask;
import elitech.vietnam.myfashion.extend.ViewPagerAdapter;
import elitech.vietnam.myfashion.gcm.GCMIntentService;
import elitech.vietnam.myfashion.view.ProductDetailView;

public class DetailFragment extends Fragment {
	// public CopyNewProductDetailView view = null;
	public ProductDetailView	view		= null;
	private MainActivity		mContext;
	private Product				mProduct;
	private int					mProductID	= -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		if (savedInstanceState != null) {
			mProductID = savedInstanceState.getInt("Id");
			mProductID = (mProductID == 0) ? -1 : mProductID;
		}
		mContext = (MainActivity) getActivity();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("Id", mProductID);
	};

	public void setProduct(Product product) {
		mProduct = product;
		mProductID = mProduct.Id;
	}

	public void setProductID(int productID) {
		mProductID = productID;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == MainActivity.LOGINACTIVITY) {

			if (resultCode == -1) {
				final int result = data.getIntExtra("Id", -1);
				if (result != -1) {
					new CallServiceTask() {
						protected void onPreExecute() {
							mContext.mLoadingDialog.show();
						};

						private Member	rs	= null;

						public void work() {
							try {
								rs = service.getInfoMemberByID(result, -1);
							} catch (IOException e) {
							} catch (XmlPullParserException e) {
							}
						};

						protected void onPostExecute(Void result) {
							mContext.mUser = rs;
							view.RefreshLayout();
							mContext.mLoadingDialog.dismiss();
							mContext.mOrder = new Order();
							mContext.mMainPageView.mPageAdapter = new ViewPagerAdapter(mContext);
							// if
							// (mContext.mFragmentManager.getBackStackEntryCount()
							// > 0)
							// mContext.mLastTitle =
							// getString(R.string.bestoftoday);
							// else
							// mContext.changeTitle(getString(R.string.bestoftoday),
							// false);

							mContext.mMainPageView.mViewPager.setAdapter(mContext.mMainPageView.mPageAdapter);

							if (mContext.mUser != null) {
								mContext.AddPreference("User", mContext.ConvertToJson(mContext.mUser));
								mContext.AddPreferenceBool("LoggedIn", true);
							} else {
								mContext.AddPreference("User", "");
								mContext.AddPreferenceBool("LoggedIn", false);
							}
						};
					}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				}
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (mProduct == null && mProductID == -1)
			throw new IllegalArgumentException("Must call setProduct or setProductID first.");

		if (mProductID == -1)
			view = new ProductDetailView(mContext, mProduct);
		// view = new CopyNewProductDetailView(mContext, mProduct);
		// view = new CopyNewProductDetailView(mContext, mProduct);
		else
			view = new ProductDetailView(mContext, mProductID);
		// view = new CopyNewProductDetailView(mContext, mProductID);
		// view = new CopyNewProductDetailView(mContext, mProduct);
		return view;
	}

	@Override
	public void onPause() {
		Log.w("Shopping", "Frag onPause");
		super.onPause();
	}

	@Override
	public void onStop() {
		Log.w("Shopping", "Frag onStop");
		super.onStop();
	}

	@Override
	public void onResume() {
		Log.w("Shopping", "Frag onResume");
		if (!mContext.mMainPageViewFragment.isHidden()) {
			Log.w("Shopping", "Frag mMainPageViewFragment unhidden");
			mContext.AddPreferenceInt("FirstStart", 0);
			Intent intent = new Intent(mContext, MainActivity.class);
			if (mContext.getIntent().getExtras() != null) {
				String notiExtras = mContext.getIntent().getExtras().getString(GCMIntentService.DATA_TAG);
				if (notiExtras != null && !notiExtras.equals(""))
					intent.putExtra(GCMIntentService.DATA_TAG, notiExtras);
			}
			mContext.startActivity(intent);
			mContext.finish();
		}
		super.onResume();
	}

	@Override
	public void onDestroy() {
		Log.w("Shopping", "Frag onDestroy");
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mProduct = null;
		mProductID = -1;
		view = null;
	}

}
